package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.BatchSetProductOnSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BatchSetProductOnSale {
	private static BatchSetProductOnSaleCall batchSetProductOnSaleCall = new BatchSetProductOnSaleCall();
	
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	/*private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	*/
	private static Integer sellerId = 3383;
	private static String sellerName = "wesper";
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架商品");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchSetProductOnSaleCall.getOpurl());
	}
	
	

	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_001() {
		Logger.start(true,"上架多个商品");
		try {
			String productId1 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_002() {
		Logger.start(true,"上架活动商品");
		try {
			String productId = ProductFactory.getFutureActivityProduct(sellerId, sellerName, MapUtil.hashMap(
																					"Product->OnSaleType", 1,
																					"Product->ValidStart",YMTDateUtil.getBeforeOrNextDay(-5,"yyyy-MM-dd'T'HH:mm:ss"),
																					"Product->ValidEnd",YMTDateUtil.getBeforeOrNextDay(-5,"yyyy-MM-dd'T'HH:mm:ss")))
																					.getProductId();
			
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			checkOnSaleTime(Arrays.asList(productId));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_003() {
		Logger.start(true,"上架FBX商品");
		try {
			String productId =  SellerProductCallService.addFBXProduct(MapUtil.hashMap("Product->OnSaleType", 2,"Product->AutoRefresh", false));
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId));
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			
			checkOnSaleTime(Arrays.asList(productId));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_004() {
		Logger.start(true,"过滤强制下架商品无法上架");
		try {
			String productId1 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			//强制下架
			String productId3 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			ymtProductsIWapper.updateCheckStatus(productId3, -1);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "过滤强制下架商品 不能上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "过滤强制下架商品 不能上架");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_005() {
		Logger.start(true,"上架商品列表中有不属于当前用户的商品 无法上架");
		try {
			String productId =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId1,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "返回code应为201");
			
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "上架商品列表中有不属于当前用户的商品 无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "上架商品列表中有不属于当前用户的商品 无法上架");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_006() {
		Logger.start(true,"库存为0商品无法上架");
		try {
			String productId1 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			//修改库存为0
			String productId3 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			SellerProductCallService.updateAllStockNumAs0(sellerId, productId3);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "库存为0商品无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "库存为0商品无法上架");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_007() {
		Logger.start(true,"删除商品 无法上架");
		try {
			String productId =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			
			SellerProductCallService.batchDeleteProduct(sellerId, Arrays.asList(productId));
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId));
			
			Logger.verifyEquals(201, result.getInt("Code"), "返回code应为201");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_008() {
		Logger.start(true,"价格与物流方式不匹配 无法上架");
		try {
			String productId1 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			//价格与物流方式不匹配 无法上架"
			String productId3 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			ymtProductsIWapper.updateCatalogStatus(productId3, 7);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "价格与物流方式不匹配 无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "价格与物流方式不匹配 无法上架");
			
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_009() {
		Logger.start(true,"商品分类（品类）有误 无法上架");
		try {
			String productId1 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			//商品分类（品类）有误 无法上架"
			String productId3 =  SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			ymtProductsIWapper.updateCategoryId(productId3, 1011);
			Date oldStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date oldEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			
			JSONObject result = batchSetProductOnSaleCall.call(sellerId,Arrays.asList(productId1,productId2,productId3));
			
			
			Logger.verifyEquals(200, result.getInt("Code"), "返回code应为200");
			
			checkOnSaleTime(Arrays.asList(productId1,productId2));
		
			Date newStartDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidStart();
			Date newEndDate = ymtProductsIWapper.selectProductByProductId(productId3).get(0).getValidEnd();
			
			AssertService.assertTime(oldStartDate, newStartDate, 10, "价格与物流方式不匹配 无法上架");
			AssertService.assertTime(oldEndDate, newEndDate, 10, "价格与物流方式不匹配 无法上架");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	public static void checkOnSaleTime(List<String> productIdList) throws Exception{
		checkOnSaleTime(productIdList,YMTDateUtil.getDate(),YMTDateUtil.getBeforeOrNextDate(7));
	}
	
	
	public static void checkOnSaleTime(List<String> productIdList, Date startDate , Date endDate ) throws Exception{
		for (String productId : productIdList) {
			//SQL YmtProducts
			@SuppressWarnings("rawtypes")
			List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
			Date svalidStartbysql = (Date)productIDsqllist.get(0).get("validStart")	;
			Date svalidEndbysql = (Date)productIDsqllist.get(0).get("validEnd");	
			AssertService.assertTime(startDate, svalidStartbysql, 600, "上架后 validStart");
			AssertService.assertTime(endDate, svalidEndbysql, 600,  "上架后 validEnd");
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
		}
	}
	
	
	
	
}
